'''Adds support for libnotify into XChat.'''
############################################################################
#    Copyright (C) 2006 by Robert Small                                    #
#    SmallR2002@Gmail.com                                                  #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################
from bcmisc import clean_text

__module_name__        = "bcnotify"
__module_version__     = "0.0.1"
__module_description__ = "Handles libnotify support."

#TODO: Festival support. Like libnotify this could be very useful for people. With ignore and focus support.
#TODO: Libnotify ignore support, make it possible to *ignore* users.
#TODO: Libnotify channel activity support. 

#  This is basically libnotify support. It uses notify-send to send the
# notification because python-notify was broken when I tried it, also, it's
# easier to write something using an app which has --help instead of being
# totally undocumented.

import xchat
import sys
import os
import subprocess
import bcmisc

if (sys.path.count(os.path.dirname(__file__)) == 0):
    sys.path.append(os.path.dirname(__file__))

#  No, don't remove the next line, bcsettings MUST be imported for this script to continue.
import bcsettings

def libnotify_send(summary, body, urgency):
    '''
    Send to libnotify.
    @param summary: summary of the message.
    @type summary: string
    @param body: the body.
    @type body: string
    @param urgency: and the urgency.
    @type urgency: string
    '''
    if (urgency != "none"):
        tempcmd = bcmisc.clean_text('notify-send "' + summary + '" "' + clean_text(body, True, False, True) + '" -u ' + urgency, False, True)
        try:
            subprocess.call(tempcmd, shell=True)
        except MemoryError:
            return None

def PRIVMSG_handle(word, word_eol, userdata): #IGNORE:W0613
    '''
    Handle pivate message.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    highlights = xchat.get_prefs('irc_extra_hilight')
    split_highlights = highlights.split(",")
    if (True in map(split_highlights.count, word[1])) or (word[1].count(xchat.get_info('nick'))):
        libnotify_send(bcmisc.clean_text(word[0], True) + " sent you an highlighted private message.", word[1], bcsettings.libnotify_urgency_hpm)
    else:
        libnotify_send(bcmisc.clean_text(word[0], True) + " sent you a private message.", word[1], bcsettings.libnotify_urgency_pm)
        

def Invite_handle(word, word_eol, userdata): #IGNORE:W0613
    '''
    Handle invite to channel.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    if len(word) > 1:
        libnotify_send("You were invited to " + bcmisc.clean_text(word[0], True), word[1] + " invited you to " + bcmisc.clean_text(word[0], True), bcsettings.libnotify_urgency_inv)

def ChannelMessageHighlight_handle(word, word_eol, userdata): #IGNORE:W0613
    '''
    Handle channel message highlight.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    if len(word) > 1:
        libnotify_send(bcmisc.clean_text(word[0], True) + " highlighted you in " + xchat.get_info('channel'), "<" + bcmisc.clean_text(word[0], True) + "> " + word[1], bcsettings.libnotify_urgency_hcm)

def ChannelMessage_handle(word, word_eol, userdata): #IGNORE:W0613
    '''
    Handle channel message.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    if len(word) > 1:
        libnotify_send(bcmisc.clean_text(word[0], True) + " spoke in " + xchat.get_info('channel'), "<" + bcmisc.clean_text(word[0], True) + "> " + word[1], bcsettings.libnotify_urgency_cm)

def ChannelActionHighligh_handle(word, word_eol, userdata): #IGNORE:W0613
    '''
    Handle channel action highlight.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    if xchat.get_info('channel') == bcmisc.clean_text(word[0], True, False):
        if len(word) > 1:
            libnotify_send(bcmisc.clean_text(word[0], True) + " highlighted you in private message.", "* " + bcmisc.clean_text(word[0], True) + " " + word[1], bcsettings.libnotify_urgency_hcm)
    else:
        if len(word) > 1:
            libnotify_send(bcmisc.clean_text(word[0], True) + " highlighted you in " + xchat.get_info('channel'), "* " + bcmisc.clean_text(word[0], True) + " " + word[1], bcsettings.libnotify_urgency_hcm)
        

def ChannelAction_handle(word, word_eol, userdata): #IGNORE:W0613
    '''
    Handle channel action.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    if xchat.get_info('channel') == bcmisc.clean_text(word[0], True, False):
        if len(word) > 1:
            libnotify_send(bcmisc.clean_text(word[0], True) + " spoke to you in private message.", "* " + bcmisc.clean_text(word[0], True) + " " + word[1], bcsettings.libnotify_urgency_cm)
    else:
        if len(word) > 1:
            libnotify_send(bcmisc.clean_text(word[0], True) + " spoke in " + xchat.get_info('channel'), "* " + bcmisc.clean_text(word[0], True) + " " + word[1], bcsettings.libnotify_urgency_cm)
        